/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package db;

import Control.DataManager;
import Model.Loan;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Jayson Kiong
 * Displays the loans that are existing in the database
 */
public class SelectLoanServlet extends HttpServlet {

    private DataManager dm = new DataManager();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       java.sql.Connection con = DBConnector.connect();
       
        
        try{
       
        Loan loan = new Loan();
        ArrayList soa = new ArrayList();
        String clientName="";
        String loanID = request.getParameter("loanno");
            
        String query = "SELECT `loan`.`loanID`, `loan`.`clientID`, `loan`.`termsID`, `loan`.`loan_amount`"
                     + " FROM quicklenddb.loan "
                     + " WHERE loan.loanID = "+loanID+" order by loan_date asc";
        
            
                PreparedStatement ps = con.prepareStatement(query);
                
                
                ResultSet rs = ps.executeQuery();
                while(rs.next())
                {
                    
                    loan.setiLoanid(rs.getInt("loanID"));
                    loan.setiClientid(rs.getInt("clientID"));
                    
                }
                
                HttpSession session = request.getSession(true);
                soa = dm.getSoaList(loan.getiClientid());
                session.setAttribute("soa", soa);
                response.sendRedirect("soa.jsp?loanno="+loan.getiLoanid());
            
        }catch(SQLException e)
        {
            System.out.println(e); 
           
        }
    

    }
}
